#define  _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
#include <assert.h>
#include <string>
#include <algorithm>
#include <functional>
namespace lw
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;
		iterator begin()
		{
			return _start;
		}
		iterator end()
		{
			return _finish;
		}
		const_iterator begin() const
		{
			return _start;
		}
		const_iterator end() const
		{
			return _finish;
		}
		vector()
			:_start(nullptr)
			,_finish(nullptr)
			,_end_of_storage(nullptr)
		{}
		vector(size_t n, const T& val = T())
			:_start(nullptr)
			, _finish(nullptr)
			, _end_of_storage(nullptr)
		{
			reserve(n);
			for (int i = 0; i < n; i++)
				push_back(val);
		}
		vector(int n, const T& val = T())
			:_start(nullptr)
			, _finish(nullptr)
			, _end_of_storage(nullptr)
		{
			reserve(n);
			for (int i = 0; i < n; i++)
				push_back(val);
		}
		template<class Iterator>
		vector(Iterator begin, Iterator end)
			:_start(nullptr)
			, _finish(nullptr)
			, _end_of_storage(nullptr)
		{
			while (begin != end)
			{
				push_back(*begin);
				begin++;
			}
		}
		vector(const vector<T>& v)
			:_start(nullptr)
			, _finish(nullptr)
			, _end_of_storage(nullptr)
		{
			vector<T> tmp(v.begin(), v.end());
			swap(tmp);
		}
		vector<T>& operator=(vector<T> v)
		{
			swap(v);
			return *this;
		}
		~vector()
		{
			delete[] _start;
			_start = _finish = _end_of_storage = nullptr;
		}
		void swap(vector<T>& v)
		{
			::swap(_start, v._start);
			::swap(_finish, v._finish);
			::swap(_end_of_storage, v._end_of_storage);
		}
		T& operator[](size_t n)
		{
			assert(n < size());

			return _start[n];
		}
		const T& operator[](size_t n) const
		{
			assert(n < size());

			return _start[n];
		}
		size_t size() const
		{
			return _finish - _start;
		}
		size_t capacity() const
		{
			return _end_of_storage - _start;
		}
		void resize(size_t n, const T& val = T())
		{
			if (n > capacity())
				reserve(n);
			if (n > size())
			{
				while (_finish < _start + n)
				{
					*_finish = val;
					_finish++;
				}
			}
			else
				_finish = _start + n;
		}
		void reserve(size_t n)
		{
			if (capacity() < n)
			{
				size_t sz = size();
				T* tmp = new T[n];
				if (_start)
				{
					for (int i = 0; i < sz; i++)
						tmp[i] = _start[i];
					delete[] _start;
				}
				_start = tmp;
				_finish = _start + sz;
				_end_of_storage = _start + n;
			}
		}
		T& front()
		{
			assert(size() > 0);
			return *_start;
		}
		T& back()
		{
			assert(size() > 0);
			return *(_finish - 1);
		}
		void push_back(const T& val)
		{
			if (_finish == _end_of_storage)
			{
				reserve(capacity() == 0 ? 4 : 2 * capacity());
			}
			*_finish = val;
			_finish++;
		}
		void pop_back()
		{
			assert(size() > 0);
			_finish--;
		}
		iterator insert(iterator pos, const T& val)
		{
			assert(pos >= _start);
			assert(pos <= _finish);
			if (_finish == _end_of_storage)
			{
				int len = pos - _start;
				reserve(capacity() == 0 ? 4 : 2 * capacity());
				pos = _start + len;
			}
			iterator tmp = _finish;
			while (tmp > pos)
			{
				*tmp = *(tmp - 1);
				tmp--;
			}
			*pos = val;
			_finish++;
			return pos;
		}
		iterator erase(iterator pos)
		{
			assert(pos >= _start);
			assert(pos < _finish);
			iterator tmp = pos + 1;
			while (tmp < _finish)
			{
				*(tmp - 1) = *tmp;
				tmp++;
			}
			_finish--;
			return pos;
		}
	private:
		T* _start;
		T* _finish;
		T* _end_of_storage;
	};
	void Test1()
	{
		vector<int> v;
		int a[] = { 1,2,3,4,5,6,7,8,9 };
		for (auto e : a)
			v.push_back(e);
		for (auto e : v)
			cout << e << " ";
		cout << endl;
	}
	void Test2()
	{
		vector<int> v;
		int a[] = { 1,2,3,4,5};
		for (auto e : a)
			v.push_back(e);
		vector<int>::iterator it = ::find(v.begin(), v.end(), 3);
		if (it != v.end())
			v.insert(it, 30);
		for (auto e : v)
			cout << e << " ";
		cout << endl;

		v.insert(v.begin(), -10);
		for (auto e : v)
			cout << e << " ";
		cout << endl;

		it = ::find(v.begin(), v.end(), 1);
		v.erase(it);
		for (auto e : v)
			cout << e << " ";
		cout << endl;

		::sort(v.begin(), v.end(), greater<int>());
		for (auto e : v)
			cout << e << " ";
		cout << endl;
	}
	void Test3()
	{
		vector<int> v;
		int a[] = { 1,2,3,4,5 };
		for (auto e : a)
			v.push_back(e);
		vector<int>::iterator it = v.begin();
		while (it != v.end())
		{
			if (*it % 2 == 0)
			{
				it = v.erase(it);
			}
			else
			{
				it++;
			}
		}
		for (auto e : v)
			cout << e << " ";
		cout << endl;
	}
	void Test4()
	{
		std::string s("hello world");
		vector<int> v(s.begin(), s.end());

		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;
	}
}

int main()
{
	lw::Test4();
	return 0;
}